import java.awt.Color;
import java.awt.Font;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.Random;

import ij.IJ;
import ij.ImagePlus;
import ij.gui.NewImage;
import ij.plugin.PlugIn;
import ij.process.ImageProcessor;



public class RedShare extends ImagePlus implements PlugIn{
	
	private ImageProcessor primeIP; 
	private int X=2800,Y=2100; // image size
	
	private ImagePlus inverted;  
	private ImageProcessor inv_ip;  

	
	private boolean[][] masks={{true,true,false,false},{false,false,true,true},{true,false,false,true},
			                   {false,true,true,false},{true,false,true,false},{false,true,false,true}};
	
	
	private Random generator=new Random(1214); 
	
	private int masque[]={0xFF0000,0x00FF00,0x0000FF}; 
	private int decal[]={16,8,0};
	private int col=0; 
	
	public void run(String arg) {
		System.out.println("go");
		
		ImagePlus imp; 
		ImagePlus blackMask; 


		imp=IJ.openImage("C:/Users/decomite/Pictures/povray/VCS/RGB/microRougeDithered.png");
		
		imp.setTitle("PrimeIP");
		
		
		
	
		System.out.println(imp); 
		primeIP=imp.getProcessor();
		X=primeIP.getWidth(); 
		Y=primeIP.getHeight(); 
		
		
        setStack(null, imp.getStack());
        setCalibration(imp.getCalibration()); 
		
        	show();
		
		
        	int memory[][]=new int[X][Y]; 
        	
        	try{
    			
    			DataInputStream in =new DataInputStream(new FileInputStream("C:/Users/decomite/Pictures/povray/VCS/RGB/BlackMaskValues.txt")); 
    		for(int i=0;i<X;i++){
    			
    			for(int j=0;j<Y;j++) {
    			
    				memory[i][j]=in.readByte();
    			
    			} 
    		}
        	}
    		catch (Exception e) {
    			System.out.println("Probleme : "+e); System.exit(0); 
    		}
		

		for(int i=0;i<100;i++) System.out.print(memory[0][i]+" "); System.out.println(); 
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.RED);
		primeIP.setLineWidth(10); 
		
	
		setProcessor("Verifying", primeIP);
		
	
	
 			inverted = NewImage.createRGBImage ("RedShare2",2*X,2*Y, 1, NewImage.FILL_WHITE);
 			inv_ip = inverted.getProcessor();
 			
 			int cmpt=0; 
 			for(int i=0;i<2*X;i+=2)
 				for(int j=0;j<2*Y;j+=2){
 					int dice=memory[i/2][j/2]; 
 					int couleur=primeIP.getPixel(i/2, j/2);
 					int cred=(couleur &0xFF0000)>>16; 
 					int cgreen=(couleur &0x00FF00)>>8; 
					int cblue=(couleur &0x0000FF); 
 					
 				
 					 
 					if(cred==0){
 					inv_ip.putPixel(i, j,  masks[dice][0] ? 0x000000:0xFF0000); 
 					inv_ip.putPixel(i, j+1,  masks[dice][1] ? 0x000000:0xFF0000); 
 					inv_ip.putPixel(i+1, j,  masks[dice][2] ? 0x000000:0xFF0000); 
 					inv_ip.putPixel(i+1, j+1,  masks[dice][3] ? 0x000000:0xFF0000);
 					}
 					else
 					{
 	 					inv_ip.putPixel(i, j,  masks[dice][0] ? 0xFF0000:0x000000); 
 	 					inv_ip.putPixel(i, j+1, masks[dice][1] ? 0xFF0000:0x000000); 
 	 					inv_ip.putPixel(i+1, j, masks[dice][2] ? 0xFF0000:0x000000); 
 	 					inv_ip.putPixel(i+1, j+1, masks[dice][3] ? 0xFF0000:0x000000);
 	 					}
 					
 				}
 					
 			// superposer avac le masque
 			
 			/*
 			for(int i=0;i<2*X;i++)
 				for(int j=0;j<2*Y;j++){
 					int dice=memory[i/2][j/2]; 
 					int couleur=inv_ip.getPixel(i, j);
 					int cwhite=(couleur &0x00FF00)>>8;
 					int di=i%2; 
 					int dj=j%2; 
 					int indice=-1; 
 					if((di==0)&&(dj==0)) indice=0; 
 					if((di==0)&&(dj==1)) indice=1; 
 					if((di==1)&&(dj==0)) indice=2; 
 					if((di==1)&&(dj==1)) indice=3;
 				    if(masks[dice][indice]) inv_ip.putPixel(i, j, 0X000000); 
 				}
 					
 			*/
 		
     
 			System.out.println("toto");
 			
 			inverted.show();
 			
 			System.out.println("end !");
	}
	
	
	

}
